<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div></div>
  
  <script>
    function speak (data) {
      var ss = new SpeechSynthesisUtterance()

      // 播放声音
      ss.text = data.msg
      ss.lang = 'zh-CN'
      ss.rate = 0.4
      speechSynthesis.speak(ss)

      ss.onend = () => {
        console.log('speak end')
        clearTimeout(this.speakTimeoutTimer)
      }

      ss.onerror = (e) => {
        console.log(e)
        console.log('error')
      }
    }

    setTimeout(() => {
      console.log('call')
      speak({ msg: 'h t m l 5 、测试文字转语音功能' })
    }, 2000)
  </script>
</body>
</html>